package labox.innovation.loginserver.socket.clientpackets;

import labox.innovation.config.Config;
import labox.innovation.loginserver.socket.LoginClient.LoginClientState;
import labox.innovation.loginserver.socket.serverpackets.GGAuth;

public class AuthGameGuard extends LoginClientPacket{
	
	private int _sessionId;
	
	private int _version;
	
	public int getSessionId() {
		return _sessionId;
	}

	public int getVersion() {
		return _version;
	}

	@Override
	protected void readImpl() {
		_sessionId = readInt();
		_version = readInt();
	}
	
	/**
	 * 校验版本方法
	 * @param versionId
	 * @return
	 */
	private boolean checkVersion(int versionId) {
		boolean result = false;
		if ((versionId >= Config.MIN_PROTOCOL_REVISION) && (versionId <= Config.MAX_PROTOCOL_REVISION)) {
			result = true;
		}
		return result;
	}

	@Override
	public void run() { //-- 版本加入校验
		if ((_sessionId == getClient().getSessionId()) && checkVersion(_version)) {//如果与登陆服务器首次init连接时分配的session id一致
			getClient().setState(LoginClientState.AUTHED_GG);  //-- 设置客户端鉴权登陆状态
			getClient().sendPacket(new GGAuth(0));  //-- 发送鉴权包
		} else if (_sessionId != getClient().getSessionId()){
			getClient().sendPacket(new GGAuth(1));
		} else if (!checkVersion(_version)) {
			getClient().sendPacket(new GGAuth(2));
		}
	}
}
